package com.example.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.student.config.BizException;
import com.example.student.config.PageParam;
import com.example.student.entity.ActivityHistory;
import com.example.student.mapper.ActivityHistoryMapper;
import com.example.student.service.ActivityHistoryService;
import com.example.student.utils.UserUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ActivityHistoryServiceImpl implements ActivityHistoryService {

        @Resource
        private ActivityHistoryMapper mapper;

        /**
        * 根据id获取单条
        * @param id
        * @author lx
        * @return
        */
        @Override
        public ActivityHistory getOne (String id){
                return mapper.selectById(id);
        }

        /**
        * 新增
        * @param activityHistory
        * @author lx
        * @return
        */
        @Override
        public boolean add (ActivityHistory activityHistory){
                List<ActivityHistory> activityHistoryList = this.mapper.selectList(new QueryWrapper<ActivityHistory>().eq("user_id",UserUtils.getId()).eq("activity_id", activityHistory.getActivityId()));
                if (activityHistoryList.size()>0){
                     throw new BizException("您已报名活动，不可重复报名！");
                }
                activityHistory.setUserId(UserUtils.getId());
                return mapper.insert(activityHistory)>0;
        }

        /**
        * 修改
        * @param activityHistory
        * @author lx
        * @return
        */
        @Override
        public boolean update (ActivityHistory activityHistory){
                return mapper.updateById(activityHistory)>0;
        }

        /**
        * 删除
        * @param id
        * @author lx
        * @return
        */
        @Override
        public boolean delete(String id){
                return mapper.deleteById(id)>0;
        }

        /**
        * 批量删除
        * @param ids
        * @author lx
        * @return
        */
        @Override
        public boolean deleteByIds(String[] ids){
                for(String id:ids){
                    mapper.deleteById(id);
                }
                return true;
        }

        /**
        * 分页查询
        * @param page
        * @author lx
        * @return
        */
        @Override
        public IPage<ActivityHistory> page(PageParam page){
            return mapper.page(new Page(page.getPage(),page.getSize()),page);
        }

}
